package jcu.ict.profaid;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class CourseDbAdapter {
	public static final String KEY_ID = "course_id";
	public static final String KEY_USER = "user_id";
	public static final String KEY_UNIVERSITY = "university";
	public static final String KEY_PROGRAM = "program";
	public static final String KEY_COURSE_CODE = "course_code";
	public static final String KEY_COURSE_NAME = "course_name";
	public static final String KEY_YEAR = "date";
	public static final String KEY_NOTES = "notes";
	private static final String DATABASE_TABLE = "course";

	private Context context;

	private DatabaseHelper DBHelper;
	private SQLiteDatabase db;

	public CourseDbAdapter(Context ctx) {
		this.context = ctx;
	}
	
	// --- open the database ---
	public CourseDbAdapter open() throws SQLException 
	{
		DBHelper = new DatabaseHelper(context);
		db = DBHelper.getWritableDatabase();
		
		if (!db.isReadOnly()) {
	        db.execSQL("PRAGMA foreign_keys = ON;");
	    }
		
		return this;
	}

	// ---closes the database---
	public void close() {
		DBHelper.close();
	}

	// ---insert a course into the database---
	public long insertCourseList(String user_id, String university, String program, String course_code, String course_name,String year, String notes ) {
		ContentValues initialValues = new ContentValues();
		initialValues.put(KEY_USER, user_id);
		initialValues.put(KEY_UNIVERSITY, university);
		initialValues.put(KEY_PROGRAM, program);
		initialValues.put(KEY_COURSE_CODE, course_code);
		initialValues.put(KEY_COURSE_NAME, course_name);
		initialValues.put(KEY_YEAR, year);
		initialValues.put(KEY_NOTES, notes);
		return db.insert(DATABASE_TABLE, null, initialValues);
	}
	
	// --- populate course from database ---
	public Cursor getAllCourse(String user_id) throws SQLException{
		
		Cursor mCursor = 
			db.query(true, DATABASE_TABLE, new String[] {KEY_COURSE_CODE,
						KEY_COURSE_NAME, KEY_YEAR, KEY_UNIVERSITY, KEY_ID}, KEY_USER + "=\"" + user_id +  "\"", null,
						null, null, null, null);
		return mCursor;
		
	}
	
	public Cursor getEditCourse(String id_course) throws SQLException{
		
		Cursor mCursor = 
				db.query(true, DATABASE_TABLE, new String[] {KEY_UNIVERSITY,KEY_PROGRAM,KEY_COURSE_CODE,KEY_COURSE_NAME,KEY_NOTES}, KEY_ID + "=\"" + id_course +  "\"", null,
						null, null, null, null);
		return mCursor;
		
	}
	
	public int DeleteCourse(String IdCourse ) throws SQLException {
		return db.delete(DATABASE_TABLE, KEY_ID + "=\"" + IdCourse +  "\"", null);
	}
	
	public boolean updateAllCourse(String CourseId, String university, String program, String Ccode, String Cname, String Notes) {
		ContentValues args = new ContentValues();
		args.put(KEY_UNIVERSITY, university);
		args.put(KEY_PROGRAM, program);
		args.put(KEY_COURSE_CODE, Ccode);
		args.put(KEY_COURSE_NAME, Cname);
		args.put(KEY_NOTES, Notes);
		return db.update(DATABASE_TABLE, args,KEY_ID + "=\"" + CourseId + "\"" , null) > 0;
	}
	
	
}
